package com.shen.volatiledemo;

import java.util.concurrent.atomic.AtomicInteger;

public class Demo03 {
   private volatile static AtomicInteger num=new AtomicInteger();

    public static void add(){
        num.getAndIncrement();//他调用本地方法对底层直接操作，CAS
    }
    public static void main(String[] args) {

        for (int i = 0; i < 20; i++) {
            new Thread(()->{
                for (int j = 0; j < 1000; j++) {
                    add();
                }
            }).start();
        }

        while (Thread.activeCount()>2){//main gc
            Thread.yield();
        }

        System.out.println(Thread.currentThread().getName()+" "+num);//main 20000
    }
}
